package cn.bqjr.eily.shiro.spring.boot;

import cn.bqjr.eily.shiro.spring.boot.entity.ShiroProperties;
import org.apache.shiro.cache.Cache;
import org.apache.shiro.cache.CacheException;
import org.apache.shiro.cache.CacheManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component("shiroRedisCacheManager")
public class ShiroRedisCacheManager implements CacheManager {

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    @Autowired
    private ShiroProperties shiroProperties;

    @Override
    public <K, V> Cache<K, V> getCache(String name) throws CacheException {
        return new RedisShiroCache<K, V>(
                shiroProperties.getRedis().getCachePrefix(),
                name,
                redisTemplate,
                shiroProperties.getRedis().getExpireTime());
    }

    public RedisTemplate<String, Object> getRedisTemplate() {
        return redisTemplate;
    }

    public void setRedisTemplate(RedisTemplate<String, Object> redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

}
